package myjava.test.bean.factory;

import java.util.LinkedList;
import java.util.Map;
import java.util.Map.Entry;

import myjava.test.xml.reader.bean.BeanContextReader;
import myjava.test.xml.reader.bean.BeanMessageReader;

public class GetAllBean extends GetBean {

	public GetAllBean(String path, boolean isRootPath) {
		super(path, isRootPath);
		initBeans();
	}
	
	@Override
	public BeanContextReader init(String path, boolean isRootPath) {
		return new BeanMessageReader(path, isRootPath, true);
	}
	
	@Override
	public <T> T createBean(Class<?> clazz) {
		// TODO Auto-generated method stub
		return super.createBean(clazz);
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public <T> T createBean(String id) {
		// TODO Auto-generated method stub
		return (T) this.objects.get(id);
	}
	
	/**
	 * 将相关类信息存进map信息中，根据map的key来获得对象
	 */
	private void initBeans(){
		for(Map<String,LinkedList<Map<String, ?>>> bean : this.context.getBeanMessage()){
			Entry<String, LinkedList<Map<String, ?>>> entry = bean.entrySet().iterator().next();
			String[] strs = entry.getKey().split("&");
			String beanName = strs[0];
			String id = strs[1];
			this.objects.put(id, this.getBean(beanName, entry.getValue()));
		}
	}

}
